home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.10 Oct 93 / Fixed-Point Math / fix.cp < prev    next >
Encoding:
Text File  |  1993-07-23  |  997 b   |  55 lines  |  [TEXT/KAHL]

  1. /***************************************
  2. * fix.cp
  3. * fixed-point number class implementation
  4. ***************************************/
  5.  
  6. #include "fix.h"
  7. #include <math.h>
  8. #include <SANE.h>
  9.  
  10. fix fix::result; // initialize static data member
  11.  
  12. fix::operator long double()
  13. {
  14.     long double result = Fix2X(n);
  15.     return (result);
  16. }
  17.  
  18. ostream& operator<<(ostream& os, const fix& f)
  19. {
  20.     os << (double) f;
  21.     return os;
  22. }
  23.  
  24. istream& operator>>(istream& is, fix& f)
  25. {
  26.     double temp;
  27.     
  28.     is >> temp;
  29.         // Since this routine reads a floating-point number, and then converts it,
  30.         // errors are handled in the same way as for reading floating-point numbers.
  31.         // The iostream library includes functions for testing and reseting the error
  32.         // state.
  33.     if (is.good())
  34.         f = (fix) temp;
  35.     return is;
  36. }
  37.  
  38. fix sin(const fix a)
  39. {
  40.     fix result;
  41.     Fract temp = FracSin(a.n);
  42.     result.n = Frac2Fix(temp);
  43.     return result;
  44. }
  45.  
  46. fix cos(const fix a)
  47. {
  48.     fix result;
  49.     Fract temp = FracCos(a.n);
  50.     result.n = Frac2Fix(temp);
  51.     return result;
  52. }
  53.  
  54.  
  55.